package com.hantang.web.json.handlers;

import java.util.Date;

import org.apache.log4j.Logger;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;

import com.google.gson.Gson;
import com.hantang.web.data.ArticleData;
import com.hantang.web.json.ds.MetaData;
import com.hantang.web.json.res.StringRes;

public class PubArticleHandler extends AbstractHandler {

	private static Logger logger = Logger.getLogger(PubArticleHandler.class);
	@Override
	public String doService(Request req, Response resp) throws Exception {
		String sid = req.getParameter("id");
		
		String sret = handleServlet(sid);
		
		return sret;
	}
	public static String handleServlet(String sid) {
		logger.info("delete article para id:"  + sid);
		
		StringRes sres = new StringRes();
		
		if(sid!=null){
			long id = Long.valueOf(sid.trim());
			
			ArticleData adata = new ArticleData();
			adata.setId(id);
			adata.setPublishTime(new Date());
			
			boolean isOk = EditArticleHandler.handle(adata);
//			ArticleDao dao = new ArticleDao();
//			 = dao.updateData(adata);
			
			if(isOk){
				sres.setData(""+ sid);
				sres.setMeta(new MetaData(MetaData.META_DATA_SUCCESS_CODE, "Succ"));				
			}else{
				sres.setMeta(new MetaData(MetaData.META_DATA_FAILED_CODE, "Failed"));
			}
		}else{
			sres.setMeta(new MetaData(MetaData.META_DATA_ERROR_PARA, "need para id!"));
		}
		Gson gson = new Gson();
		String sret = gson.toJson(sres);
		return sret;
	}

}
